package miroshnichenko.gallery.web;

import miroshnichenko.gallery.dao.UserDao;
import miroshnichenko.gallery.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.security.Principal;

@Controller
@RequestMapping("/profile")
public class ProfileController {

    private Logger log = LoggerFactory.getLogger(ProfileController.class);

    @Autowired
    UserDao userDao;

    @RequestMapping(method = RequestMethod.GET)
    public String viewProfile(Model model, Principal principal) {

        User user = userDao.getUserByEmail(principal.getName());

        model.addAttribute("first_name", user.getFirstName());
        model.addAttribute("last_name", user.getLastName());
        model.addAttribute("email", user.getEmail());

        String userStatus = user.getAdminRights() ? "Administrator" : "User";
        model.addAttribute("admin_status", userStatus);

        return "profile";
    }
}
